const { sqlite3 } = require('../../mysql')

// 管理文章列表数据
const getindex = async (ctx) => {
  console.log('ctx.query:', ctx.query)
  console.log('ctx.query.index:', ctx.query.index)
  let formData = ctx.query,
    offset = (formData.page - 1) * formData.limit,
    keyword = formData.keyword,
    queryIndex = ctx.query.index
  menusData = {}
  ;(limitData = {}), (data = {})
  console.log('queryIndex:', queryIndex)
  console.log('=index', queryIndex === 'index')
  console.log('=1', queryIndex !== 'index')

  if (queryIndex === 'index') {
    menusData = await sqlite3('menus').where('is_show', 1).select()
    limitData = await sqlite3('article').count({ total: '*' })
    data = await sqlite3('article')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = limitData[0].total
  } else if (queryIndex !== 'index') {
    limitData = await sqlite3('article').count({ total: '*' })
    data = await sqlite3('article')
      .where({
        nav: queryIndex,
      })
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = limitData[0].total
  } else if (keyword) {
    data = await sqlite3('article')
      .where('title', 'LIKE', '%' + keyword + '%')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = data.length
    console.log('data:', data)
  } else {
    limitData = await sqlite3('article').count({ total: '*' })
    data = await sqlite3('article')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = limitData[0].total
  }

  if (data) {
    // console.log('limitData:', limitData)
    // console.log('data:', data)
    ctx.body = {
      code: 200,
      list: data,
      menus: menusData,
      total,
      result: true,
    }
  } else {
    ctx.body = {
      code: false,
    }
  }
}

// 管理平台获取留言列表数据
const getArtList = async (ctx) => {
  console.log('ctx.query:', ctx.query)
  let formData = ctx.query,
    offset = (formData.page - 1) * formData.limit,
    keyword = formData.keyword,
    limitData = {},
    data = {}
  console.log('keyword:', keyword)
  if (keyword) {
    data = await sqlite3('article')
      .where('title', 'LIKE', '%' + keyword + '%')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = data.length
    console.log('data:', data)
  } else {
    data = await sqlite3('article')
      .rightOuterJoin('menus', function() {
        this
        .on('article.nav', '=', 'menus.navto')
      })
      .orderBy('id', 'desc')
      .limit(formData.limit)
      .select()
    console.log('data:', data)
    limitData = await sqlite3('article').count({ total: '*' })
    total = limitData[0].total
    // data = await sqlite3('article')
    //   .orderBy('id', 'desc')
    //   .select()
    //   .limit(formData.limit)
    //   .offset(offset)
  }
  if (data) {
    // console.log('limitData:', limitData)
    // console.log('data:', data)
    ctx.body = {
      code: 200,
      list: data,
      total,
      result: true,
    }
  } else {
    ctx.body = {
      code: false,
    }
  }
}

const getArticle = async (ctx) => {
  let articleId = ctx.query.id
  console.log('articleId:', articleId)
  data = await sqlite3('article').where('id', articleId).select()
  if (data) {
    let item = data[0],
      updata = await sqlite3('article')
        .where('id', articleId)
        .update('click', (item.click += 1))
    ctx.body = {
      code: 200,
      item,
      result: true,
    }
  } else {
    ctx.body = {
      code: false,
    }
  }
}

module.exports = { getindex, getArtList, getArticle }
